package service

import (
	"fmt"
	"gateway/client/chessgame"
	"gateway/tools/security"
	"github.com/gin-gonic/gin"
	"log"
	"math/rand"
	"net/http"
	"strconv"
	"time"
)

type createGameRoom struct {
	Token  string `json:"token"`
	RoomId int    `json:"room_id"`

	IsPve bool `json:"is_pve"`
}
type showGameRoomList struct {
	Token string
}

func ShowGameRoom(ct *gin.Context) {
	json := showGameRoomList{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	action, err := security.VerifyAction(json.Token)
	if err != nil {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": err.Error()})
		return
	}
	_, ok := security.UserMap[action.UserID]
	if !ok {
		ct.JSON(http.StatusAccepted, gin.H{"status": http.StatusBadGateway, "message": "账号未登录"})
		return
	}
	list, err := chessgame.GetChessRoomList(action.UserID)
	if err != nil {
		ct.JSON(http.StatusBadRequest, list)
		return
	}
	ct.JSON(http.StatusOK, list)
}

type showGameCombatRecord struct {
	Token  string
	RoomId int
}

func ShowGameCombatRecord(ct *gin.Context) {
	json := showGameCombatRecord{}
	err := ct.BindJSON(&json)
	if err != nil || json.RoomId == 0 || json.Token == "" {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	userInfo, err := security.VerifyAction(json.Token)
	if err != nil {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": err.Error()})
		return
	}
	_, ok := security.UserMap[userInfo.UserID]
	if !ok {
		ct.JSON(http.StatusAccepted, gin.H{"status": http.StatusBadGateway, "message": "账号未登录"})
		return
	}
	record, err := chessgame.ShowCombatRecord(userInfo.UserID, json.RoomId)
	if err != nil {
		ct.JSON(http.StatusBadRequest, record)
		return
	}
	ct.JSON(http.StatusOK, record)
}

var pveCheck = make(map[int]bool)

func CreateGameRoom(ct *gin.Context) {
	json := createGameRoom{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	userInfo, err := security.VerifyAction(json.Token)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest,
			gin.H{"status": http.StatusBadRequest, "message": err.Error()})
		return
	}
	_, ok := security.UserMap[userInfo.UserID]
	if !ok {
		ct.JSON(http.StatusAccepted, gin.H{"status": http.StatusBadGateway, "message": "账号未登录"})
		return
	}
	room, err := chessgame.CreateChessGameRoom(json.RoomId, userInfo.UserID, json.IsPve)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, room)
		return
	}
	if json.IsPve {
		pveCheck[json.RoomId] = true
	}
	ct.JSON(http.StatusOK, room)
}
func JoinGameRoom(ct *gin.Context) {
	json := createGameRoom{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}

	userInfo, err := security.VerifyAction(json.Token)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest,
			gin.H{"status": http.StatusBadRequest, "message": err.Error()})
		return
	}
	_, ok := security.UserMap[userInfo.UserID]
	if !ok {
		ct.JSON(http.StatusAccepted, gin.H{"status": http.StatusBadGateway, "message": "账号未登录"})
		return
	}
	room, err := chessgame.JoinRoom(json.RoomId, userInfo.UserID)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, room)
		return
	}
	ct.JSON(http.StatusOK, room)
}

type playGame struct {
	RoomId  int    `json:"room_id" `
	Token   string `json:"token" `
	X       int    `json:"x"`
	Y       int    `json:"y"`
	IfLucky bool   `json:"if_lucky"`
}

func PlayGame(ct *gin.Context) {
	json := playGame{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.RoomId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	if json.IfLucky {
		x := json.X
		y := json.Y
		rand.Seed(time.Now().UnixNano())
		json.X = rand.Intn(10)
		json.Y = rand.Intn(10)
		ct.JSON(http.StatusOK, gin.H{"message": "随缘下棋模式启动", "data": "Old coordinate (" + strconv.Itoa(x) + "," + strconv.Itoa(y) + ")\nNew coordinate\n(" + strconv.Itoa(json.X) + "," + strconv.Itoa(json.Y) + ")"})
	}
	userInfo, err := security.VerifyAction(json.Token)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest,
			gin.H{"status": http.StatusBadRequest, "message": err.Error()})
	}
	_, ok := security.UserMap[userInfo.UserID]
	if !ok {
		ct.JSON(http.StatusAccepted, gin.H{"status": http.StatusBadGateway, "message": "账号未登录"})
		return
	}
	game, err := chessgame.PlayGame(json.RoomId, userInfo.UserID, json.X, json.Y, userInfo.FullName)
	if err != nil {
		fmt.Println(err.Error() + "1111")

		if err.Error() == "rpc error: code = Unknown desc = please play the chess piece again" && (pveCheck[json.RoomId] == true) {
			for true {
				if json.IfLucky || pveCheck[json.RoomId] == true {
					/*x := json.X
					y := json.Y*/
					rand.Seed(time.Now().UnixNano())
					json.X = rand.Intn(10)
					json.Y = rand.Intn(10)
					//ct.JSON(http.StatusOK, gin.H{"message": "随缘下棋模式启动", "data": "Old coordinate (" + strconv.Itoa(x) + "," + strconv.Itoa(y) + ")\nNew coordinate\n(" + strconv.Itoa(json.X) + "," + strconv.Itoa(json.Y) + ")"})
				}
				game, err := chessgame.PlayGame(json.RoomId, userInfo.UserID, json.X, json.Y, userInfo.FullName)
				if err == nil {
					ct.JSON(http.StatusOK, game)
					return
				}
				if err.Error() != "rpc error: code = Unknown desc = please play the chess piece again" {
					break
				}
			}
		}
		log.Println(err)
		ct.JSON(http.StatusBadRequest, game)
		return
	}
	ct.JSON(http.StatusOK, game)
}
